package com.netease.nim.demo.session.viewholder;

import android.graphics.drawable.AnimationDrawable;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.netease.nim.demo.common.media.audioplayer.Playable;
import com.netease.nim.demo.session.audio.MessageAudioControl;
import com.netease.nim.demo.util.TimeUtil;
import com.netease.nimlib.sdk.msg.attachment.AudioAttachment;
import com.netease.nimlib.sdk.msg.constant.AttachStatusEnum;
import com.netease.nimlib.sdk.msg.model.IMMessage;
import com.shwread.android.qysw10000136.R;

public class MsgViewHolderAudioRight extends MsgViewHolder {

	public TextView audioLength;
	public View playAudioButton;
	private MessageAudioControl mBaseAudioControl;
	private LinearLayout playAudioProgressBar;
	private ImageView audioImageView;

	@Override
	protected int getResId() {
		return R.layout.audio_message_view_right_item;
	}

	@Override
	protected int getViewHolderDirection() {
		return VIEW_HOLDER_RIGHT;
	}
	
	@Override
	public void inflate() {
		super.inflate();

		audioLength = (TextView)view.findViewById(R.id.textViewAudioLength);
		playAudioButton = (View)view.findViewById(R.id.buttonViewStartPlayAudio);
		playAudioProgressBar = (LinearLayout)view.findViewById(R.id.layoutPlayAudio);
		audioImageView = (ImageView)view.findViewById(R.id.imageViewAudio);
		longClickView = playAudioButton;

		mBaseAudioControl = MessageAudioControl.getInstance(context);
	}

	@Override
	public void refresh(Object item) {
		super.refresh(item);

		final IMMessage messageHistory = messageItem.getMessage();
        AudioAttachment msgAttachment = (AudioAttachment) messageHistory.getAttachment();

		String audioPath = msgAttachment.getPath();
		if (TextUtils.isEmpty(audioPath)
				&& messageHistory.getAttachStatus() == AttachStatusEnum.transferred) {
			// TODO: huangjun  redownload();
		}

		long milliseconds = msgAttachment.getDuration();			
		setAudioBubbleWidth(milliseconds);
		
		playAudioButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(mBaseAudioControl != null){
					mBaseAudioControl.startPlayAudioDelay(CLICK_TOPLAY_AUDIO_DELAY, messageHistory, onPlayListener);
				}																		
			}
		});

		if (!isMessagePlaying(mBaseAudioControl, messageHistory)) {
			if (mBaseAudioControl.getAudioControlListener() != null && mBaseAudioControl.getAudioControlListener().equals(onPlayListener)) {
				mBaseAudioControl.changeAudioControlListener(null);
			}

			setAudioLength(milliseconds);
			audioImageView.setVisibility(View.VISIBLE);				
			playAudioProgressBar.setVisibility(View.GONE);	
			for (int i = 0; i < playAudioProgressBar.getChildCount(); i++) {					
				View view = playAudioProgressBar.getChildAt(i);
				AnimationDrawable animationDrawable = (AnimationDrawable) view.getBackground();
				stopAnimation(animationDrawable, playAudioButton, false);					
			}				
		}
		else {
			mBaseAudioControl.changeAudioControlListener(onPlayListener);
			
			audioImageView.setVisibility(View.GONE);
			playAudioProgressBar.setVisibility(View.VISIBLE);
			for (int i = 0; i < playAudioProgressBar.getChildCount(); i++) {					
				View view = playAudioProgressBar.getChildAt(i);
				AnimationDrawable animationDrawable = (AnimationDrawable) view.getBackground();
				startAnimation(animationDrawable, playAudioButton, false);					
			}		
		}
	}

	private void setAudioLength(long milliseconds) {
		long seconds = TimeUtil.getSecondsByMilliseconds(milliseconds);

		if (seconds >= 1) {
			audioLength.setText(seconds+"\"");			
		}
		else {
			audioLength.setText("");			
		}														
	}

	private void setAudioBubbleWidth(long milliseconds) {
		long seconds = TimeUtil.getSecondsByMilliseconds(milliseconds);

		int currentBubbleWidth = calculateBubbleWidth(seconds, MAX_AUDIO_TIME_SECOND);

		LayoutParams layoutParams = playAudioButton.getLayoutParams();
		layoutParams.width = currentBubbleWidth;
		playAudioButton.setLayoutParams(layoutParams);
	}
	
	private MessageAudioControl.AudioControlListener onPlayListener = new MessageAudioControl.AudioControlListener() {

		@Override
		public void updatePlayingProgress(Playable playable, long curPosition) {
			long seconds = curPosition;	
			if (seconds > playable.getDuration()) {
				return;
			}
			setAudioLength(seconds);
		}

		@Override
		public void onAudioControllerReady(Playable playable) {
            if (eventListener != null) {
                eventListener.onAudioStartPlay();
            }

			audioImageView.setVisibility(View.GONE);
			playAudioProgressBar.setVisibility(View.VISIBLE);
			for (int i = 0; i < playAudioProgressBar.getChildCount(); i++) {					
				View view = playAudioProgressBar.getChildAt(i);
				AnimationDrawable animationDrawable = (AnimationDrawable) view.getBackground();
				startAnimation(animationDrawable, playAudioButton, false);					
			}
		}

		@Override
		public void onEndPlay(Playable playable) {
            if (eventListener != null) {
                eventListener.onAudioEndPlay();
            }

			setAudioLength(playable.getDuration());

			audioImageView.setVisibility(View.VISIBLE);
			playAudioProgressBar.setVisibility(View.GONE);	
			for (int i = 0; i < playAudioProgressBar.getChildCount(); i++) {					
				View view = playAudioProgressBar.getChildAt(i);
				AnimationDrawable animationDrawable = (AnimationDrawable) view.getBackground();
				stopAnimation(animationDrawable, playAudioButton, false);					
			}	
		}
	};

    @Override
    public void reclaim() {
        super.reclaim();
        if (mBaseAudioControl.getAudioControlListener() != null && mBaseAudioControl.getAudioControlListener().equals(onPlayListener)) {
            mBaseAudioControl.changeAudioControlListener(null);
        }
    }
}
